﻿using SS.WPF.Framework.Restriction;

namespace SS.WPF.Framework.ValidationRule
{
    using System.Globalization;
    using System.Windows.Controls;

    /// <summary>
    /// The class implement abstract class ValidationRule is used to check the validity of user input.
    /// </summary>
    public class RestrictionValidationRule : ValidationRule
    {
        /// <summary>
        /// Refer to instance of IRestriction that is used to check validity
        /// </summary>
        private readonly IRestriction _restriction;

        /// <summary>
        /// Create instance of RestrictionValidationRule with specified IRestriction
        /// </summary>
        /// <param name="restriction">Instance of IRestriction</param>
        public RestrictionValidationRule(IRestriction restriction)
        {
            _restriction = restriction;
            this.ValidatesOnTargetUpdated = true;
        }

        #region Overrides of ValidationRule

        /// <summary>
        /// When overridden in a derived class, performs validation checks on a value.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Windows.Controls.ValidationResult"/> object.
        /// </returns>
        /// <param name="value">The value from the binding target to check.</param><param name="cultureInfo">The culture to use in this rule.</param>
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            return this._restriction.Validate(value)
                       ? ValidationResult.ValidResult
                       : new ValidationResult(false, this._restriction.Description);
        }

        #endregion
    }
}